﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Libs.Content;

public partial class Pages_Content_NewsSub_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        if (!IsPostBack)
        {
            News _News = new News();
            _News = _News.Get(AppUtils.Request("id"));
            ftbContent.Text = _News.Content;

            BindNewsSub();
        }
    }

    private void BindNewsSub()
    {
        var _NewsSub = new NewsSub();
        rptNewsSub.DataSource = _NewsSub.GetList(AppUtils.Request("id"));
        rptNewsSub.DataBind();
    }

    // Cập nhật điểm báo
    protected void btNewsSubEdit_Click(object sender, EventArgs e)
    {
        NewsSub _NewsSub = new NewsSub();
        for (int i = 0; i < rptNewsSub.Items.Count; i++)
        {
            Label lbl = (Label)rptNewsSub.Items[i].FindControl("lblNewsSubID");
            TextBox txtTitle = (TextBox)rptNewsSub.Items[i].FindControl("txtTitle");
            TextBox txtLead = (TextBox)rptNewsSub.Items[i].FindControl("txtLead");

            _NewsSub.NewsSubID = Convert.ToInt32(lbl.Text);
            _NewsSub = _NewsSub.Get();
            _NewsSub.Title = txtTitle.Text;
            _NewsSub.Lead = txtLead.Text;
            _NewsSub.Update();
        }

    }

    // Xóa điểm báo
    protected void btNewsSubDelete_Click(object sender, EventArgs e)
    {
        NewsSub _NewsSub = new NewsSub();
        for (int i = 0; i < rptNewsSub.Items.Count; i++)
        {
            CheckBox cbx = (CheckBox)rptNewsSub.Items[i].FindControl("cbxStatus");
            Label lbl = (Label)rptNewsSub.Items[i].FindControl("lblNewsSubID");
            if (cbx.Checked)
            {
                _NewsSub.Delete(Convert.ToInt32(lbl.Text));
            }
        }

        BindNewsSub();
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        if (txtNewsSubTitle.Text.Trim().Length == 0 || txtNewsSubLead.Text.Trim().Length == 0)
        {
            Message.Alert(Page, "Bạn cần nhật tiêu đề và nội dung điểm báo!");
        }
        NewsSub _NewsSub = new NewsSub();
        _NewsSub.NewsID = AppUtils.Request("id");
        _NewsSub.UserID = AppUtils.UserID;
        _NewsSub.Title = txtNewsSubTitle.Text.Trim();
        _NewsSub.Lead = txtNewsSubLead.Text.Trim();
        _NewsSub.Add();
        txtNewsSubTitle.Text = "";
        txtNewsSubLead.Text = "";

        News _News = new News();
        _News = _News.Get(AppUtils.Request("id"));
        _News.Content = Resources.Config.NewsSub.Replace("$Title$", _NewsSub.Title).Replace("$Lead$", _NewsSub.Lead) + _News.Content;
        ftbContent.Text = _News.Content;

        _News.Update();
        BindNewsSub();
    }
}